package com.dingding.service.account.core.dao.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.Useraccount;
import com.dingding.common.core.beans.special.Specialuseraccount;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.service.account.core.dao.IUserAccountDao;

/**
 * 
 * @ClassName: UserAccountDaoImpl 
 * @Description: 用户账户表表DAO 
 * @author CHEHAUNBO 
 * @date 2016年3月30日 下午4:21:48
 * 
 */
@Component("userAccountDao")
public class UserAccountDaoImpl extends BaseDaoImpl<Useraccount> implements IUserAccountDao {

	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;

	/**
	 * 
	 * @Title: addUserAccount 
	 * @Description: 添加用户账户信息 
	 * @param userLoginId 登录id 
	 * @return 
	 * @author CHEHUANBO
	 * 
	 */
	public void addUserAccount(String userLoginId) {

		Useraccount useraccount = new Useraccount();
		// 为用户账户表设置主键
		useraccount.setUaId(ToolUtil.getUid());
		// 用户登录ID
		useraccount.setLoginId(userLoginId);
		// 账户余额
		useraccount.setCurrencyAmount(new BigDecimal(0));
		// 账户状态
		useraccount.setStatus(1);
		// 累计收入
		useraccount.setTotalIncome(new BigDecimal(0));
		// 累计提现
		useraccount.setTotalDeposit(new BigDecimal(0));
		// 累计支出
		useraccount.setTotalExpend(new BigDecimal(0));
		try {
			// 向用户账户表插入一条记录
			super.save(useraccount);
		} catch (Exception e) {
			log.error("添加用户账户信息出现异常！", e);
		}

	}

	/**
	 * @Description: 获取用户账户信息
	 * @param loginId 用户登录id
	 * @author fengshuonan
	 * @return Useraccount
	 */
	@Override
	@Transactional(readOnly = true)
	public Useraccount getUserAccount(String loginId) {
		// 添加订单Id作为查询条件
		Criterion criterion = Restrictions.eq("loginId", loginId);

		// 根据订单Id查询订单信息
		Useraccount useraccount = null;

		try {
			useraccount = this.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("根据订单Id获取订单信息时出现异常  类：" + this.getClass() + " getUserAccount()", e);
		}

		return useraccount;
	}

	/**
	 * 
	 * @Description: 扣除剩余金钱
	 * @param userId 用户ID
	 * @param money 金额
	 * @author CHEHUANBO
	 * @return boolean true 扣除成功   false 扣除失败
	 * 
	 */
	@Override
	public boolean deductRemainMoney(String userId, BigDecimal money) {

		Useraccount useraccount = this.getUserAccount(userId);

		if (useraccount == null) {
			return false;
		}

		if (useraccount.getCurrencyAmount() != null && useraccount.getCurrencyAmount().compareTo(money) >= 0) {
			useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().subtract(money));
			return true;
		}

		return false;
	}

	
	/**
	 * 
	 * @Description: 扣除剩余金钱（出租车）
	 * @param userId 用户ID
	 * @param money 金额
	 * @author YHQ
	 * @since V2.5.4
	 * @return boolean true 扣除成功   false 扣除失败
	 * 
	 */
	@Override
	public boolean taxiDeductRemainMoney(String userId, BigDecimal money) {

		Useraccount useraccount = this.getUserAccount(userId);

		if (useraccount == null) {
			return false;
		}

		if (useraccount.getCurrencyAmount() != null &&  useraccount.getCurrencyAmount().compareTo(money) >= 0) {
			useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().subtract(money));
			useraccount.setTotalExpend(useraccount.getTotalExpend().add(money));
			return true;
		}

		return false;
	}
	
	
	/**
	 * 
	 * @Description: 充值
	 * @param userId 用户ID
	 * @param money 金额
	 * @author CHEUANBO
	 * @return boolean true 扣除成功   false 扣除失败
	 * 
	 */
	@Override
	public boolean rechargMoney(String userId, BigDecimal money) {

		Useraccount useraccount = this.getUserAccount(userId);

		if (useraccount == null) {
			return false;
		}

		if (useraccount.getCurrencyAmount() != null) {
			useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().add(money));
			useraccount.setTotalIncome(useraccount.getTotalIncome().add(money));
			return true;
		}

		return false;
	}

	/**
	 * 
	 * @Title: specialRecharge  给专车司机转账
	 * @param userId 用户Id
	 * @param money 金额
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean specialRecharge(String userId, BigDecimal money) {

		Criterion criterion = Restrictions.eq("userId", userId);

		try {
			List<Specialuseraccount> useraccounts = criteriaBaseDao.queryCriteriaList(Specialuseraccount.class,
					criterion);

			if (useraccounts != null && useraccounts.size() > 0) {

				Specialuseraccount specialuseraccount = useraccounts.get(0);

				specialuseraccount.setCurrencyAmount(specialuseraccount.getCurrencyAmount().add(money));

				criteriaBaseDao.update(specialuseraccount);

			} else {
				log.error("给专车司机转账时传入的用户Id不存在用户账户信息  用户Id: " + userId);
				return false;
			}

		} catch (Exception e) {
			log.error("获取专车司机的用户账户信息时出现异常   类：" + this.getClass() + " specialRecharge()", e);
			return false;
		}

		return true;
	}

	
	/**
	 * 
	 * @Title: refunds 
	 * @Description: 给乘客退款(顺风车) 
	 * @param loginId 用户Id
	 * @param fee 退款金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean refunds(String loginId, BigDecimal fee){
		
		Criterion criterion = Restrictions.eq("loginId", loginId);

		try {
			List<Useraccount> useraccounts = criteriaBaseDao.queryCriteriaList(Useraccount.class,criterion);

			if (useraccounts != null && useraccounts.size() > 0) {

				Useraccount useraccount = useraccounts.get(0);

				useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().add(fee));

				criteriaBaseDao.update(useraccount);

			} else {
				log.error("给顺风车司机退款时传入的用户Id不存在用户账户信息  用户Id: " + loginId);
				return false;
			}

		} catch (Exception e) {
			log.error("获取顺风车司机退款时用户账户信息时出现异常   类：" + this.getClass() + " refunds()", e);
			return false;
		}
		
		return true;
	}
	
	
	/**
	 * 
	 * @Title: transferAccounts 
	 * @Description: 转账操作
	 * @param loginId 用户Id
	 * @param fee 转账金额
	 * @author YHQ 
	 * @since V2.5.4
	 * @return boolean    返回类型
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean transferAccounts(String loginId, BigDecimal fee){
		
		Criterion criterion = Restrictions.eq("loginId", loginId);

		try {
			List<Useraccount> useraccounts = criteriaBaseDao.queryCriteriaList(Useraccount.class,criterion);

			if (useraccounts != null && useraccounts.size() > 0) {

				Useraccount useraccount = useraccounts.get(0);

				// 增加转账金额
				useraccount.setCurrencyAmount(useraccount.getCurrencyAmount().add(fee));
				// 增加累计收入
				useraccount.setTotalIncome(useraccount.getTotalIncome().add(fee));

				criteriaBaseDao.update(useraccount);

			} else {
				log.error("给顺风车司机退款时传入的用户Id不存在用户账户信息  用户Id: " + loginId);
				return false;
			}

		} catch (Exception e) {
			log.error("获取顺风车司机退款时用户账户信息时出现异常   类：" + this.getClass() + " refunds()", e);
			return false;
		}
		
		return true;
	}
	
}
